/*
 * $Id: MailInfo.java,v 1.3 2009/01/29 05:16:29 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util.mail;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.mail.Address;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import com.ge.healthcare.autosc.common.ASCUtil;

/**
 * 
 * @author 212042946
 * This class holds email information.
 */
public class MailInfo {

	// smtp server host name
	private String serverHostName = null;
	// Email recipient list
	private String[] recipients = null;
	// Email CC recipient list
	private String[] ccRecipients = null;
	// Email BCC recipient list
	private String[] bccRecipients = null;
	// Sender email Id
	private String senderAddress = null;
	// Email subject
	private String emailSubject = null;
	// Email body content
	private String emailBody = null;
	// Set of attachments
	private Set<Attachment> attachments = Collections.synchronizedSet(new HashSet<Attachment>());
	
	/**
	 * Constructor
	 */
	public MailInfo(){};
	
	/**
	 * Constructor
	 * @param serverHostName
	 * @param recipients
	 * @param ccRecipients
	 * @param bccRecipients
	 * @param senderAddress
	 * @param emailSubject
	 * @param emailBody
	 * @param attachments
	 */
	public MailInfo(
			String serverHostName,
			String[] recipients,
			String[] ccRecipients,
			String[] bccRecipients,
			String senderAddress,
			String emailSubject,
			String emailBody,
			Set<Attachment> attachments) {
		this.serverHostName = serverHostName;
		this.recipients = recipients;
		this.ccRecipients = ccRecipients;
		this.bccRecipients = bccRecipients;
		this.senderAddress = senderAddress;
		this.emailSubject = emailSubject;
		this.emailBody = emailBody;
		this.attachments.addAll(attachments);
	}
	
	/**
	 * Constructor
	 */
	public MailInfo(
			String serverHostName,
			String recipient,
			String ccRecipient,
			String bccRecipient,
			String senderAddress,
			String emailSubject,
			String emailBody,
			Set<Attachment> attachments) {
		this.serverHostName = serverHostName;
		if(recipients != null) {
			recipients = new String[1];
			recipients[0] = recipient;
		}
		if(ccRecipients != null) {
			ccRecipients = new String[1];
			ccRecipients[0] = ccRecipient;
		}
		
		if(bccRecipients != null) {
			bccRecipients = new String[1];
			bccRecipients[0] = bccRecipient;
		}
		this.senderAddress = senderAddress;
		this.emailSubject = emailSubject;
		this.emailBody = emailBody;
		this.attachments.addAll(attachments);
	}
	
	/**
	 * Get list of recipients address
	 * @return list of recipient
	 */
	public String[] getRecipients() {
		return recipients;
	}
	
	/**
	 * Get list of recipient address
	 * @return
	 * @throws AddressException
	 */
	public Address[] getRecipientAddresses() 
		throws AddressException {
		InternetAddress[] addresses = null;
		if(recipients != null) {
			addresses = new InternetAddress[recipients.length];
			for(int idx = 0;idx < recipients.length;idx++) {
				addresses[idx] = new InternetAddress(recipients[idx]);
			}
		} 
		return addresses;
	}
	
	/**
	 * Set recipient address list
	 * @param recipients
	 */
	public void setRecipients(String[] recipients) {
		this.recipients = recipients;
	}
	
	/**
	 * Determine any recipient being set
	 * @return true/false
	 */
	public boolean anyRecipients() {
		if(this.recipients != null) {
			if(this.recipients.length > 0) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Get send email address
	 * @return sender address
	 */
	public String getSenderAddress() {
		return senderAddress;
	}
	
	/**
	 * Set sender email address
	 * @param senderAddress
	 */
	public void setSenderAddress(String senderAddress) {
		this.senderAddress = senderAddress;
	}
	
	/**
	 * Determine if any sender email address being set
	 * @return true/false
	 */
	public boolean anySenderAddress() {
		return (ASCUtil.isNullOrEmpty(senderAddress)?false:true);
	}
	
	/**
	 * Get email subject
	 * @return email subject
	 */
	public String getEmailSubject() {
		return emailSubject;
	}

	/**
	 * Set email subject
	 * @param emailSubject
	 */
	public void setEmailSubject(String emailSubject) {
		this.emailSubject = emailSubject;
	}
	
	/**
	 * Get email body content
	 * @return email body content
	 */
	public String getEmailBody() {
		return emailBody;
	}
	
	/**
	 * Set email body content
	 * @param emailBody
	 */
	public void setEmailBody(String emailBody) {
		this.emailBody = emailBody;
	}
	
	/**
	 * Determine if any email body content being set
	 * @return true/false
	 */
	public boolean anyEmailBody() {
		return (this.emailBody != null)?true:false;
	}
	
	/**
	 * Get list of email attachment
	 * @return list of attachment
	 */
	public Set<Attachment> getAttachements() {
		return attachments;
	}
	
	/**
	 * Add attachment to existing list of attachment
	 * @param attachment
	 */
	public void addAttachment(Attachment attachment) {
		this.attachments.add(attachment);
	}
	
	/**
	 * Add list of attachments to existing list of attachment
	 * @param attachments
	 */
	public void addAttachments(Set<Attachment> attachments) {
		this.attachments.addAll(attachments);
	}
	
	/**
	 * Determine if any attachment being set
	 * @return true/false
	 */
	public boolean anyAttachment() {
		if(attachments.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * Get list of CC recipient
	 * @return list of CC recipient
	 */
	public String[] getCcRecipients() {
		return ccRecipients;
	}
	
	/**
	 * Get list of CC recipient
	 * @return list of CC recipient
	 * @throws AddressException
	 */
	public Address[] getCcRecipientAddresses()
		throws AddressException {
		InternetAddress[] addresses = null;
		if(ccRecipients != null) {
			addresses = new InternetAddress[ccRecipients.length];
			for(int idx = 0;idx < ccRecipients.length;idx++) {
				addresses[idx] = new InternetAddress(ccRecipients[idx]);
			}
		} 
		return addresses;
	}
	
	/**
	 * Set list of CC recipient
	 * @param ccRecipients
	 */
	public void setCcRecipients(String[] ccRecipients) {
		this.ccRecipients = ccRecipients;
	}

	/**
	 * Determine if any CC recipient being set
	 * @return true/false
	 */
	public boolean anyCcRecipients() {
		if(this.ccRecipients != null) {
			if(this.ccRecipients.length > 0) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * GEt the SMTP server host name
	 * @return server host name
	 */
	public String getServerHostName() {
		return serverHostName;
	}

	/**
	 * SEt SMTP server host name
	 * @param serverHostName
	 */
	public void setServerHostName(String serverHostName) {
		this.serverHostName = serverHostName;
	}

	/**
	 * Get list of BCC recipient
	 * @return list of BCC recipient
	 */
	public String[] getBccRecipients() {
		return bccRecipients;
	}
	
	/**
	 * Get list of BCC recipient
	 * @return list of BCC recipient
	 */
	public Address[] getBccRecipientAddresses()
		throws AddressException {
		InternetAddress[] addresses = null;
		if(bccRecipients != null) {
			addresses = new InternetAddress[bccRecipients.length];
			for(int idx = 0;idx < bccRecipients.length;idx++) {
				addresses[idx] = new InternetAddress(bccRecipients[idx]);
			}
		} 
		return addresses;
	}
	
	/**
	 * Set list of BCC recipient
	 * @param bccRecipients
	 */
	public void setBccRecipients(String[] bccRecipients) {
		this.bccRecipients = bccRecipients;
	}
	
	/**
	 * Determine if any BCC recipient being set
	 * @return true/false
	 */
	public boolean anyBccRecipients() {
		if(this.bccRecipients != null) {
			if(this.bccRecipients.length > 0) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Print the content of the instance
	 */
	public String toString() {
		StringBuffer msg = new StringBuffer();
		msg.append("Smpt Host: " + this.getServerHostName());
		msg.append("\n");
		msg.append("Subject: " + this.getEmailSubject());
		msg.append("\n");
		if(recipients != null) {
			StringBuffer recipientsStr = new StringBuffer();
			msg.append("Recipients: \n");
			for(int idx=0; idx < recipients.length; idx++) {
				recipientsStr.append(recipients[idx]);
				recipientsStr.append("\n");
			}
			msg.append(recipientsStr.toString());
		}
		msg.append("\n");
		StringBuffer attachmentNames = new StringBuffer();
		Iterator<Attachment> attIter = attachments.iterator();
		while(attIter.hasNext()) {
			Attachment attachment = (Attachment)attIter.next();
			attachmentNames.append(attachment.getAttachmentName());
			attachmentNames.append("\n");
		}
		msg.append("\n");
		msg.append("Mail Body:\n" + this.getEmailBody());
		return msg.toString();
	}
}
